package com.example.controller;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.dto.AdminDto;
import com.example.dto.AdminPageDto;
import com.example.entity.Admin;
import com.example.service.AdminService;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin //跨域问题
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    /**
     * admin分页查询
     * @param adminPageDto
     * @return
     */
    @GetMapping("/search")
    public Result<PageResult> findBySearch(AdminPageDto adminPageDto){
        log.info("admin分页查询{}",adminPageDto);
        PageResult page =  adminService.findBySearch(adminPageDto);
        return Result.success(page);
    }

    @PostMapping
    public Result<String> add(@RequestBody Admin admin){
        log.info("新增管理员账号:{}",admin);
        adminService.add(admin);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除{}",id);
        adminService.deleteById(id);
        return Result.success();
    }

    @PutMapping("/edit")
    public Result edit(@RequestBody Admin admin){
        adminService.edit(admin);
        return Result.success();
    }
}
